library(rvest)
library(tidyverse)

sle.gse <- read_csv('mission/geo.sle.gse.csv')

get_gse_sumgpl <- function(acc){
url <- str_glue("https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc={acc}")
vest <- read_html(url)

summary <- vest |>
  html_element('tr:nth-child(6) td:nth-child(2)') |>
  html_text2()

GPL <- vest |>
  html_element('tr:nth-child(23) a') |>
  html_text2()

tibble(gse = acc, summary = summary, GPL = GPL)
}

sumgpl <- sle.gse$gseid |>
  map(safely(get_gse_sumgpl, quiet = F), .progress = T)

sumgpl |>
  list_transpose() |>
  pluck('result') |>
  write_csv('sle.gse.summary.csv')

# reutils --------
library(reutils)

get_gse_sumgpl <- function(acc){
  query <- str_glue('{acc} "gse"[Entry Type]')
  esearch(query, 'gds') |>
    esummary() |>
    content('parsed')
}

sumgpl <- sle.gse$gseid |>
  map(safely(get_gse_sumgpl, quiet = F), .progress = T)

sumgpl.tib <- sumgpl |>
  list_transpose() |>
  pluck('result') |>
  map(safely(\(x)select(x, Accession, summary, GPL))) |>
  list_transpose()

sumgpl.tib$result |>
  list_rbind() |>
  write_csv('gse.reutil.csv')

sumgpl.tib$error |>
  set_names(sle.gse$gseid) |>
  discard(is_null)
